﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;
using Volo.Abp.Domain.Services;

namespace Himp.Outpatient
{
    /// <summary>
    /// 门诊处方
    /// </summary>
    public class OtpRxManger : DomainService
    {
        private readonly IRepository<OtpRxRec, Guid> _otpRxRecRepository;
        private readonly IRepository<OtpRxRecDetl, Guid> _otpRxRecDetlRepository;

        public OtpRxManger(IRepository<OtpRxRec, Guid> otpRxRecRepository, IRepository<OtpRxRecDetl, Guid> otpRxRecDetlRepository)
        {
            _otpRxRecRepository = otpRxRecRepository;
            _otpRxRecDetlRepository = otpRxRecDetlRepository;
        }

        /// <summary>
        /// 创建门诊处方记录
        /// </summary>
        /// <param name="clinicPrescriptionRecord"></param>
        /// <returns></returns>
        public async Task<OtpRxRec> CreateRecAsync(Guid otpMdtrtRecId, Guid otpRxTypeId
            , Guid bilgDeptId, Guid bilgDrId)
        {
            OtpRxRec otpRxRec = new OtpRxRec(Guid.NewGuid().ToString("N"), otpMdtrtRecId, otpRxTypeId
                , bilgDeptId, bilgDrId);

            return otpRxRec;
        }

        /// <summary>
        /// 获取门诊处方支付记录
        /// </summary>
        /// <returns></returns>
        public async Task<List<OtpRxRec>> GetOtpRxPayList(string otpNo)
        {
            var iQuery = await _otpRxRecRepository.WithDetailsAsync();
            iQuery = iQuery.Where(o => o.RxStats == EnumRxStats.已审核);
            iQuery = iQuery.Where(o=>o.OtpMdtrtRec.OtpNo == otpNo);

            return iQuery.ToList();
        }

        /// <summary>
        /// 获取待支付的门诊处方明细
        /// </summary>
        /// <param name="OtpRxOrdIds"></param>
        /// <returns></returns>
        public async Task<List<OtpRxRecDetl>> GetOtpNotPayRxRecDetlList(List<Guid> otpRxOrdIds)
        {
            var iQuery = await _otpRxRecDetlRepository.GetQueryableAsync();
            iQuery = iQuery.Where(i => otpRxOrdIds.Contains(i.Id));
            
            return iQuery.ToList();
        }

        ///// <summary>
        ///// 添加处方明细
        ///// </summary>
        ///// <param name="clinicPrescriptionRecordId"></param>
        ///// <param name="clinicPrescriptionDetails"></param>
        ///// <returns></returns>
        //public OtpRxRecDetl CreateClinicPrescriptionDetail(Guid clinicPrescriptionRecordId, OtpRxRecDetl clinicPrescriptionDetail)
        //{
        //    clinicPrescriptionDetail.ClinicPrescriptionRecordId = clinicPrescriptionRecordId;
        //    clinicPrescriptionDetail.BeginDate = DateTime.Now.Date;

        //    return clinicPrescriptionDetail;
        //}
    }
}
